package com.xujiang.common.util;

import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AndroidRuntimeException;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xujiang.common.R;

/**
 * Created by xujiang on 2017/4/24.
 */

public class ToolBarHelper {
    //http://www.cnblogs.com/mengdd/p/5590634.html
    //http://blog.csdn.net/u012702547/article/details/50932090

    private Toolbar toolbar;
    private TextView toolbarTitle;
    private TextView toolbarSubTitle;
    private View toolbarRight;
    private View toolbarLeft;
    private static ActionBar actionBar;

    public static ActionBar getActionBar() {
        return actionBar;
    }

    public static void setActionBar(ActionBar actionBar) {
        ToolBarHelper.actionBar = actionBar;
    }

    public TextView getToolbarSubTitle() {
        return toolbarSubTitle;
    }

    public void setToolbarSubTitle(TextView toolbarSubTitle) {
        this.toolbarSubTitle = toolbarSubTitle;
    }

    public View getToolbarLeft() {
        return toolbarLeft;
    }

    public void setToolbarLeft(int resId) {
        ((ImageButton) this.toolbarLeft).setImageResource(resId);
    }

    public android.support.v7.widget.Toolbar getToolbar() {
        return toolbar;
    }

    public void setToolbar(Toolbar toolbar) {
        this.toolbar = toolbar;
    }

    public TextView getToolbarTitle() {
        return toolbarTitle;
    }

    public void setToolbarTitle(String toolbarTitle) {
        this.toolbarTitle.setText(toolbarTitle);
    }
    public void setToolbarSubTitle(String toolbarTitle) {
        this.toolbarSubTitle.setText(toolbarTitle);
    }


    public View getToolbarRight() {
        return toolbarRight;
    }

    public void setToolbarRight(int resId) {
        ((ImageButton) this.toolbarRight).setImageResource(resId);
    }

    public static ToolBarHelper with(@NonNull AppCompatActivity activity) {
        Toolbar toolbar = UIUtils.findViewById(activity, R.id.m_toolbar);
        activity.setSupportActionBar(toolbar);   ////将ToolBar设置成ActionBar
        actionBar = activity.getSupportActionBar();
        if (actionBar == null) {
            throw new AndroidRuntimeException("SupportActionBar is null");
        }

        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        return new ToolBarHelper(toolbar);

    }

    public static ToolBarHelper with(@NonNull View view) {

        return new ToolBarHelper((Toolbar) UIUtils.findViewById(view, R.id.m_toolbar));
    }

    public ToolBarHelper(Toolbar toolbar) {
        if (toolbar == null) {
            throw new IllegalArgumentException("Toolbar is null");
        }
        this.toolbar = toolbar;
        toolbarTitle = (TextView) toolbar.findViewById(R.id.m_toolbar_title);
        toolbarRight = toolbar.findViewById(R.id.m_toolbar_right);
        toolbarLeft = toolbar.findViewById(R.id.m_toolbar_left);
        toolbarSubTitle = (TextView) toolbar.findViewById(R.id.m_toolbar_subtitle);
    }

    /**
     * 以下三个设置是否显示自定义的view
     * 默认为隐藏
     */
    public void setToolbarTitleVisibility(boolean isVisibility) {
        if (isVisibility) {
            toolbarTitle.setVisibility(View.VISIBLE);
        } else {
            toolbarTitle.setVisibility(View.GONE);
        }
    }

    public void setToolbarSubTitleVisibility(boolean isVisibility) {
        if (isVisibility) {
            toolbarSubTitle.setVisibility(View.VISIBLE);
        } else {
            toolbarSubTitle.setVisibility(View.GONE);
        }
    }

    public void setToolbarRight(boolean isVisibility) {
        if (isVisibility) {
            toolbarRight.setVisibility(View.VISIBLE);
        } else {
            toolbarRight.setVisibility(View.GONE);
        }
    }

    public void setToolbarLeft(boolean isVisibility) {
        if (isVisibility) {
            toolbarLeft.setVisibility(View.VISIBLE);
        } else {
            toolbarLeft.setVisibility(View.GONE);
        }
    }

    /**
     * 自定义显示开关
     */
    public void toggleVisibilityToolbarTitle() {
        UIUtils.toggleVisibility(toolbarTitle);
    }

    public void toggleVisibilityToolbarSubTitle() {
        UIUtils.toggleVisibility(toolbarSubTitle);
    }

    public void toggleVisibilityToolbarLeft() {
        UIUtils.toggleVisibility(toolbarLeft);
    }

    public void toggleVisibilityToolbarRight() {
        UIUtils.toggleVisibility(toolbarRight);
    }

    /***************** 隔断 ********************************/

    public void setOnclicklister(final Callback<View> callback) {
        toolbarTitle.setEnabled(true);
        toolbarTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onSuccess(v);
            }
        });

        toolbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onSuccess(v);
            }
        });

        toolbarRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onSuccess(v);
            }
        });

    }

}

/**
 * 应用实例
 *
 * private ToolBarHelper toolBarHelper;
 * private Toolbar toolbar;
 *
 * //在oncreateview中
 * toolBarHelper = ToolBarHelper.with(view);
 * toolbar = toolBarHelper.getToolbar();
 * setToolbar(toolbar);
 *
 *
 * //设置和监听
 * private void setToolbar(Toolbar toolbar) {
 * if(toolbar == null){
 * Log.i("TAG","mytoolbar 为空");
 * }
 * toolbar.setNavigationIcon(R.mipmap.ic_launcher); //设置导航栏图标
 * toolbar.setNavigationOnClickListener(new View.OnClickListener() {
 *
 * @Override public void onClick(View v) {
 *
 * Toast.makeText(getContext(),"test",Toast.LENGTH_SHORT).show();
 * }
 * });
 * toolbar.setLogo(R.mipmap.user_portrait_hint_sm);  //设置app logo
 *
 * toolbar.setTitle("Title test can show how long"); //设置主标题
 * toolbar.setSubtitle("Subtitle test can sho how long"); //设置子标题
 *
 * toolbar.setOnClickListener(new View.OnClickListener() {
 * @Override public void onClick(View v) {
 * Toast.makeText(getContext(),"test",Toast.LENGTH_SHORT).show();
 * }
 * });
 *
 * toolbar.inflateMenu(R.menu.base_toolbar_menu);  //设置右上角的填充菜单
 * toolbar.setOnMenuItemClickListener(
 * new Toolbar.OnMenuItemClickListener() {
 * @Override public boolean onMenuItemClick(MenuItem item) {
 * switch (item.getItemId()){
 * case R.id.action_search:
 * Toast.makeText(getContext() ,"action_search" , Toast.LENGTH_SHORT).show();
 * break;
 * case R.id.action_notification:
 * Toast.makeText(getContext() ,"action_notification" , Toast.LENGTH_SHORT).show();
 * break;
 *
 * case R.id.action_item1:
 * Toast.makeText(getContext() ,"action_item1" , Toast.LENGTH_SHORT).show();
 * break;
 *
 * case R.id.action_item2:
 * Toast.makeText(getContext() ,"action_item2" , Toast.LENGTH_SHORT).show();
 *
 * break;
 * }
 *
 * return true;
 * }
 * }
 * );
 * }
 */
